<template>
  <el-dialog v-el-drag-dialog :title="!form.roleId?'新建角色':'编辑角色'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="roleForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-form-item hidden>
        <el-input v-model="form.roleId" hidden />
      </el-form-item>
      <el-card>
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="form.roleName" />
        </el-form-item>
        <el-form-item label="权限字符" prop="roleKey">
          <el-input v-model="form.roleKey" />
        </el-form-item>
        <el-form-item label="角色排序" prop="roleSort">
          <el-input-number v-model="form.roleSort" :min="1" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { roleLoad, roleSave } from '@/api/system/role'

export default {
  name: 'RoleFormDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        roleId: undefined,
        roleName: undefined,
        roleKey: undefined,
        roleSort: 1,
        status: '0',
        remark: ''
      },
      rules: {
        roleName: [{ required: true, message: '请填写角色名称' }],
        roleKey: [{ required: true, message: '请填写权限字符' }]
      },
      optionOfStatus: [],
    }
  },
  methods: {
    init(roleId) {
      this.hasLoaded = false
      this.form.roleId = roleId || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(roleId)
        this.getDictData()
      })
    },
    loadForm(roleId) {
      roleLoad({ 'id': roleId }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['system:role:edit'])
      }).catch(() => {})
    },
    getDictData() {
      this.$dictGroup('sys_normal_disable').then(response => {
        this.optionOfStatus = response
      })
    },
    onSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          roleSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onCancel() {
      this.$refs.roleForm.resetFields()
      this.form.roleSort = 1
    },
    onClose() {
      this.onCancel()
      this.visible = false
    }
  }
}
</script>
